//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.gui.fixed;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// Referenced classes of package org.jpgolay.gui.fixed:
//            LabelAndComboBox, LabelAndDateComboBox
public class SearchBox extends JPanel {

    public SearchBox() {
        jTextField = null;
        jButtonRight = null;
        val = "N/A";
        jComboBox = null;
        selectedString = "";
        selectedIndex = -1;
        searchJPanel = null;
        jButtonLeft = null;
        mainJPanel = null;
        advanceSearchJPanel = null;
        labelAndComboBox = null;
        labelAndDateComboBox = null;
        labelAndDateComboBox1 = null;
        initialize();
    }

    private void initialize() {
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(300, 27));
        add(getMainJPanel(), "North");
        setSize(getMainJPanel().getSize());
    }

    private JTextField getJTextField() {
        if (jTextField == null) {
            jTextField = new JTextField();
            jTextField.setText(val);
            jTextField.setPreferredSize(new Dimension(100, 23));
            jTextField.addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                    setVal(jTextField.getText());
                }
            });
        }
        return jTextField;
    }

    private JButton getJButtonRight() {
        if (jButtonRight == null) {
            jButtonRight = new JButton();
            jButtonRight.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/me/images/search18x18.png")));
            jButtonRight.setPreferredSize(new Dimension(20, 20));
            jButtonRight.setContentAreaFilled(false);
            jButtonRight.setBorderPainted(false);
            jButtonRight.setToolTipText("Lancer la recherche");
        }
        return jButtonRight;
    }

    private JComboBox getJComboBox() {
        if (jComboBox == null) {
            jComboBox = new JComboBox();
            jComboBox.setPreferredSize(new Dimension(150, 23));
            jComboBox.setModel(new DefaultComboBoxModel(list));
            jComboBox.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    String oldString = selectedString;
                    int oldIndex = selectedIndex;
                    selectedIndex = jComboBox.getSelectedIndex();
                    selectedString = (String) jComboBox.getSelectedItem();
                    firePropertyChange("selectedIndex", oldIndex, selectedIndex);
                    firePropertyChange("selectedString", oldString, selectedString);
                }
            });
        }
        return jComboBox;
    }

    private JPanel getSearchJPanel() {
        if (searchJPanel == null) {
            FlowLayout flowLayout1 = new FlowLayout();
            searchJPanel = new JPanel();
            searchJPanel.setLayout(flowLayout1);
            flowLayout1.setHgap(2);
            flowLayout1.setVgap(2);
            searchJPanel.add(getJButtonLeft(), null);
            searchJPanel.add(getJComboBox(), null);
            searchJPanel.add(getJTextField(), null);
            searchJPanel.add(getJButtonRight(), null);
        }
        return searchJPanel;
    }

    private JButton getJButtonLeft() {
        if (jButtonLeft == null) {
            jButtonLeft = new JButton();
            jButtonLeft.setBorderPainted(false);
            jButtonLeft.setContentAreaFilled(false);
            jButtonLeft.setPreferredSize(new Dimension(20, 20));
            jButtonLeft.setToolTipText("Fonctions avanc\351es");
            jButtonLeft.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/arrowRight18x18.png")));
            jButtonLeft.setRolloverEnabled(false);
            jButtonLeft.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    RepaintManager rm = RepaintManager.currentManager(getMainJPanel());
                    if (mainJPanel.getComponentCount() == 1) {
                        changeSize(328, (int) (27D + getAdvanceSearchJPanel().getSize().getHeight()));
                        mainJPanel.add(getAdvanceSearchJPanel(), "Center");
                        rm.addInvalidComponent(advanceSearchJPanel);
                        mainJPanel.repaint();
                        javax.swing.Icon old = jButtonLeft.getIcon();
                        javax.swing.Icon icon = new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/arrowDown18x18.png"));
                        jButtonLeft.setIcon(icon);
                        firePropertyChange("icon", old, icon);
                    } else {
                        mainJPanel.remove(getAdvanceSearchJPanel());
                        changeSize(328, 27);
                        mainJPanel.repaint();
                        javax.swing.Icon old = jButtonLeft.getIcon();
                        javax.swing.Icon icon = new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/arrowRight18x18.png"));
                        jButtonLeft.setIcon(icon);
                        firePropertyChange("icon", old, icon);
                    }
                }
            });
        }
        return jButtonLeft;
    }

    private void changeSize(int width, int height) {
        mainJPanel.setPreferredSize(new Dimension(width, height));
        setSize(width, height);
    }

    private JPanel getMainJPanel() {
        if (mainJPanel == null) {
            mainJPanel = new JPanel();
            mainJPanel.setLayout(new BorderLayout());
            mainJPanel.setSize(328, 27);
            mainJPanel.add(getSearchJPanel(), "North");
        }
        return mainJPanel;
    }

    private JPanel getAdvanceSearchJPanel() {
        if (advanceSearchJPanel == null) {
            advanceSearchJPanel = new JPanel();
            GridLayout gridLayout1 = new GridLayout();
            advanceSearchJPanel.setLayout(gridLayout1);
            advanceSearchJPanel.setPreferredSize(new Dimension(328, 81));
            advanceSearchJPanel.setSize(328, 81);
            gridLayout1.setRows(3);
            gridLayout1.setColumns(1);
            advanceSearchJPanel.add(getLabelAndComboBox(), null);
            advanceSearchJPanel.add(getLabelAndDateComboBox1(), null);
            advanceSearchJPanel.add(getLabelAndDateComboBox(), null);
        }
        return advanceSearchJPanel;
    }

    public void setButtonLabelLeft(String label) {
        getJTextField().setText(label);
    }

    public String getButtonLabelLeft() {
        return getJTextField().getText();
    }

    public void setButtonLabelRight(String label) {
        getJButtonRight().setText(label);
    }

    public String getButtonLabelRight() {
        return getJButtonRight().getText();
    }

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        String old = val;
        this.val = val;
        firePropertyChange("val", old, val);
        getJTextField().setText(val);
    }

    public String[] getList() {
        return list;
    }

    public void setList(String list[]) {
        String old[] = list;
        this.list = list;
        firePropertyChange("list", old, list);
        jComboBox.setModel(new DefaultComboBoxModel(list));
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(int selectedIndex) {
        int old = selectedIndex;
        this.selectedIndex = selectedIndex;
        firePropertyChange("selectedIndex", old, selectedIndex);
        jComboBox.setSelectedIndex(selectedIndex);
    }

    public void setAdvanceSearch(JPanel advanceSearchJPanel) {
        this.advanceSearchJPanel = advanceSearchJPanel;
    }

    public JPanel getAdvanceSearch() {
        return getAdvanceSearchJPanel();
    }

    public JButton getButtonLeft() {
        return getJButtonLeft();
    }

    public String getSelectedString() {
        return selectedString;
    }

    public void setSelectedString(String selectedString) {
        String old = selectedString;
        this.selectedString = selectedString;
        firePropertyChange("selectedString", old, selectedString);
        jComboBox.setSelectedItem(selectedString);
    }

    public JTextField getTextField() {
        return getJTextField();
    }

    public JButton getButtonRight() {
        return getJButtonRight();
    }

    public JComboBox getComboBox() {
        return getJComboBox();
    }

    private LabelAndComboBox getLabelAndComboBox() {
        if (labelAndComboBox == null) {
            labelAndComboBox = new LabelAndComboBox();
        }
        return labelAndComboBox;
    }

    private LabelAndDateComboBox getLabelAndDateComboBox() {
        if (labelAndDateComboBox == null) {
            labelAndDateComboBox = new LabelAndDateComboBox();
        }
        return labelAndDateComboBox;
    }

    private LabelAndDateComboBox getLabelAndDateComboBox1() {
        if (labelAndDateComboBox1 == null) {
            labelAndDateComboBox1 = new LabelAndDateComboBox();
        }
        return labelAndDateComboBox1;
    }
    private JTextField jTextField;
    private JButton jButtonRight;
    private String val;
    private JComboBox jComboBox;
    private String list[] = {
        "N/A"
    };
    private String selectedString;
    private int selectedIndex;
    private JPanel searchJPanel;
    private JButton jButtonLeft;
    private JPanel mainJPanel;
    private JPanel advanceSearchJPanel;
    private LabelAndComboBox labelAndComboBox;
    private LabelAndDateComboBox labelAndDateComboBox;
    private LabelAndDateComboBox labelAndDateComboBox1;
}
